/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelCalendario.java
 *
 * Created on 15/04/2011, 12:29:32 AM
 */

package Interface.Panel;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

/**
 *
 * @author Sebastian
 */
public class PanelCalendario extends javax.swing.JPanel {

    /** Creates new form PanelCalendario */
    public PanelCalendario(ActionListener listener) {
        initComponents();        
        iniciarCalendario();
        inicializarBotones();
        agregarBotones();
        agregarListeners(listener);
    }

    private void iniciarCalendario(){
        botones = new ArrayList<JButton>();
        Calendar cal = new GregorianCalendar();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);

        int i;
        Locale idi = new Locale("ES");
        labelMes.setText(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, idi).toUpperCase());
        for(i=0;i<7;i++){
            JLabel label = new JLabel(dias[i]);
            label.setHorizontalAlignment(SwingConstants.CENTER);
            panelCalendario.add(label);
        }

        for(i = 1;i < cal.get(Calendar.DAY_OF_WEEK);i++){
            JLabel label = new JLabel(" ");
            panelCalendario.add(label);
        }
    }

    private void inicializarBotones(){
        Calendar cal = new GregorianCalendar();
        int i;
        for(i = 0;i < cal.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
            JButton boton = new JButton(Integer.toString(i+1));            
            botones.add(boton);
        }

        deshabilitarQuincena();
    }

    private void agregarBotones(){
        for(JButton boton: botones){
            panelCalendario.add(boton);
        }
    }

    private void agregarListeners(ActionListener listener){
        for(JButton boton: botones){
            boton.addActionListener(listener);
        }
    }

    private void deshabilitarQuincena(){
        Calendar cal = Calendar.getInstance();
        int diaActual= cal.get(Calendar.DATE);
        if(diaActual>=15){
            for(int i=0;i<15;i++){
                botones.get(i).setEnabled(false);
            }
        }
        else{
            for(int i=15;i<cal.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
                botones.get(i).setEnabled(false);
            }
        }
    }

    public ArrayList<JButton> getBotones() {
        return botones;
    }

    public void setBotones(ArrayList<JButton> botones) {
        this.botones = botones;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelMes = new javax.swing.JLabel();
        panelCalendario = new javax.swing.JPanel();

        labelMes.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        labelMes.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelMes.setText("jLabel1");
        labelMes.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        panelCalendario.setLayout(new java.awt.GridLayout(0, 7));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(labelMes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelCalendario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE))
                .addContainerGap(387, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(labelMes, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(219, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel labelMes;
    private javax.swing.JPanel panelCalendario;
    // End of variables declaration//GEN-END:variables
    private ArrayList<JButton> botones;
    private String[] dias = {"Dom","Lun","Mar","Mie","Jue","Vie","Sab"};

}
